<template>
  <div>
    <el-dialog
        :title="'审核' + title"
        :visible.sync="dialogVisible"
        width="800px"
        :close-on-click-modal="false"
        @close="onClose"
    >
      <div style="margin-bottom: 20px;">
        <h3>申请人信息</h3>
        <el-descriptions :column="2" border>
          <template v-for="(value, key) in displayFields">
            <el-descriptions-item
              v-if="formData[key] && !['id', 'status', 'review_remark', 'reviewer', 'create_time', 'update_time'].includes(key)"
              :key="key"
              :label="getFieldLabel(key)"
              :span="key === 'work_unit' || key === 'company' || key === 'business_scope' || key === 'project_name' ? 2 : 1">
              {{ formatFieldValue(key, formData[key]) }}
            </el-descriptions-item>
          </template>
        </el-descriptions>
      </div>

      <div style="margin-bottom: 20px;">
        <h3>审核信息</h3>
        <SubmitForm
            ref="submitFormRef"
            :formList="formList"
            :ruleForm="ruleForm"
            :rules="rules"
            :loading="submitLoading"
            :showClose="false"
            @save="onSubmit"
        />
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="onClose">取消</el-button>
        <el-button type="primary" @click="onSubmit" :loading="submitLoading">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { http } from "@/api";
import SubmitForm from "@/components/SubmitForm";

export default {
  name: "youthStartupLoanReview",
  components: {
    SubmitForm
  },
  data() {
    return {
      dialogVisible: false,
      submitLoading: false,
      formData: {},
      title: '',
      ruleForm: {
        status: '',
        reviewer: '',
        review_remark: ''
      },
      rules: {
        status: [
          { required: true, message: '请选择审核状态', trigger: 'change' }
        ],
        reviewer: [
          { required: true, message: '请输入审核人', trigger: 'blur' }
        ]
      },
      formList: [
        {
          label: '审核状态',
          prop: 'status',
          name: 'select',
          options: [
            { value: 1, label: '审核通过' },
            { value: 2, label: '审核拒绝' }
          ]
        },
        {
          label: '审核人',
          prop: 'reviewer',
          name: 'input',
          placeholder: '请输入审核人姓名'
        },
        {
          label: '审核备注',
          prop: 'review_remark',
          name: 'rich',
          placeholder: '请输入审核备注'
        }
      ]
    };
  },
  methods: {
    open(data) {
      this.dialogVisible = true;
      this.formData = { ...data };
      this.title = this.getModuleTitle();
      this.ruleForm = {
        status: data.status || '',
        reviewer: data.reviewer || '',
        review_remark: data.review_remark || ''
      };
    },
    onClose() {
      this.dialogVisible = false;
      this.formData = {};
      this.ruleForm = {
        status: '',
        reviewer: '',
        review_remark: ''
      };
    },
    async onSubmit() {
      try {
        this.submitLoading = true;

        const submitData = {
          ...this.ruleForm
        };

        await http.API_MODULE.update(this.formData.id, submitData);

        this.$message.success('审核成功');
        this.$emit('get-page');
        this.onClose();
      } catch (error) {
        console.error('审核失败:', error);
        if (error.response && error.response.data && error.response.data.message) {
          this.$message.error(error.response.data.message);
        } else {
          this.$message.error('审核失败');
        }
      } finally {
        this.submitLoading = false;
      }
    },
    getModuleTitle() {
      const titles = {
        settlementSubsidy: '安家补贴申请',
        startupSubsidy: '创业补贴申请',
        householdMove: '户籍迁移申请',
        skillSubsidy: '技能补贴申请',
        youthStartupLoan: '青年创业贷款申请',
        jobSubsidy: '求职补贴申请',
        youthHostel: '青年驿站申请'
      };
      return titles['youthStartupLoan'] || '申请';
    },
    getFieldLabel(key) {
      const labels = {
        name: '姓名',
        id_card: '身份证号',
        gender: '性别',
        phone: '联系电话',
        education: '学历',
        work_unit: '工作单位',
        company: '就业企业',
        project_name: '创办项目名称',
        business_scope: '经营范围',
        subsidy_category: '类别',
        employment_count: '带动就业人数',
        applicant_name: '申请人姓名',
        move_person_name: '迁移人姓名',
        move_person_id_card: '迁移人身份证号',
        move_person_phone: '迁移人联系电话',
        relationship: '关系',
        move_in_location: '迁入地',
        certificate_type: '证书类型',
        certificate_level: '证书等级',
        occupation: '职业(工种)',
        subsidy_amount: '补贴金额',
        subsidy_count: '申请次数',
        business_entity_name: '经营实体名称',
        credit_code: '统一社会信用代码',
        ethnicity: '民族',
        household_location: '户籍所在地',
        photo: '一寸彩照',
        employment_cert_number: '《就业创业证》号码',
        personnel_category: '人员类别'
      };
      return labels[key] || key;
    },
    formatFieldValue(key, value) {
      if (key === 'gender') {
        return value === '男' ? '男' : value === '女' ? '女' : value;
      }
      if (key === 'subsidy_amount' && value) {
        return '¥' + value;
      }
      if (key === 'certificate_type') {
        return value === 1 ? '职业资格证书' : value === 2 ? '职业技能等级证书' : value;
      }
      if (key === 'certificate_level') {
        const levels = {1: '初级', 2: '中级', 3: '高级'};
        return levels[value] || value;
      }
      if (key === 'status') {
        const statusMap = {0: '待审核', 1: '审核通过', 2: '审核拒绝'};
        return statusMap[value] || value;
      }
      return value;
    }
  },
  computed: {
    displayFields() {
      return Object.keys(this.formData).filter(key => 
        this.formData[key] && 
        !['id', 'status', 'review_remark', 'reviewer', 'create_time', 'update_time'].includes(key)
      );
    },
    API_MODULE() {
      return http['youthStartupLoan'];
    }
  }
};
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
